home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / ContainerIO.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.5 KB  |  77 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: ContainerIO.py,v 1.1.1.1 1998/08/18 13:07:56 sjoerd Exp $
  4. #
  5. # a class to read from a container file
  6. #
  7. # History:
  8. #    95-06-18 fl    Created
  9. #    95-09-07 fl    Added readline(), readlines()
  10. #
  11. # Copyright (c) Secret Labs AB 1997.
  12. # Copyright (c) Fredrik Lundh 1995.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16.  
  17. # --------------------------------------------------------------------
  18. # Return a restricted file object allowing a user to read and
  19. # seek/tell an individual file within a container file (for example
  20. # a TAR file).
  21.  
  22. class ContainerIO:
  23.  
  24.     def __init__(self, fh, offset, length):
  25.     self.fh = fh
  26.     self.pos = 0
  27.     self.offset = offset
  28.     self.length = length
  29.     self.fh.seek(offset)
  30.  
  31.     def isatty(self):
  32.         return 0
  33.  
  34.     def seek(self, offset, mode = 0):
  35.     if mode == 1:
  36.         self.pos = self.pos + offset
  37.     elif mode == 2:
  38.         self.pos = self.length + offset
  39.     else:
  40.         self.pos = offset
  41.     # clamp
  42.     self.pos = max(0, min(self.pos, self.length))
  43.     self.fh.seek(self.offset + self.pos)
  44.  
  45.     def tell(self):
  46.     return self.pos
  47.  
  48.     def read(self, n = 0):
  49.     if n:
  50.         n = min(n, self.length - self.pos)
  51.     else:
  52.         n = self.length - self.pos
  53.     if not n: # EOF
  54.         return ""
  55.     self.pos = self.pos + n
  56.     return self.fh.read(n)
  57.  
  58.     def readline(self):
  59.     s = ""
  60.     while 1:
  61.         c = self.read(1)
  62.         if not c:
  63.         break
  64.         s = s + c
  65.         if c == "\n":
  66.         break
  67.     return s
  68.  
  69.     def readlines(self):
  70.     l = []
  71.     while 1:
  72.         s = self.readline(self)
  73.         if not s:
  74.         break
  75.         l.append(s)
  76.     return l
  77.